#include "GAF_Face.h"

namespace GAF
{
	Face::Face()
	{
		m_nGType	= GRAPHICS_FACE;
		m_pimgFace	= NULL;
	}

	Face::Face(unsigned int nWidth, unsigned int nHeight)
	{
		GetSize_a().nWidth = nWidth;
		GetSize_a().nHeight = nHeight;
		m_pimgFace = Image::Create(nWidth, nHeight);
	}

	Face::~Face()
	{
		if(m_pimgFace != NULL)
			Image::Destroy(m_pimgFace);

		m_lstGraphicses.clear();
	}

	bool Face::Init(Bundle* pbdlParam)
	{
		return OnInit(pbdlParam);
	}

	void Face::AddGraphics(Graphics* pGraphics, bool isBack)
	{
		if(pGraphics == NULL)
			int ddd = 0;

		GAF_CHECK(pGraphics != NULL);

		if(pGraphics->GetDockGroup() == this)
			RemoveGraphics(pGraphics);

		if(isBack)
			m_lstGraphicses.push_front(pGraphics);
		else
			m_lstGraphicses.push_back(pGraphics);

		pGraphics->DockGroup(this);

		RefreshSize();
	}

 	void Face::AddGraphicses(Graphics** pGraphics, COUNT nCount, bool isBack)
 	{
 		for(UINT _i = 0; _i < nCount; _i++)
 			AddGraphics(pGraphics[_i], isBack);
 	}

	void Face::RemoveGraphics(Graphics* pGraphics)
	{
		GAF_CHECK(pGraphics != NULL);

		m_lstGraphicses.remove(pGraphics);
		pGraphics->DockGroup(NULL);
	}

	void Face::ClearGraphics()
	{
		if(m_lstGraphicses.size() > 0)
		{
			for(GraphicsList::const_iterator _cit = m_lstGraphicses.begin();
				_cit != m_lstGraphicses.end(); _cit++)
			{
				(*_cit)->DockGroup(NULL);
			}
			m_lstGraphicses.clear();
		}
	}

	Pointer Face::Foreach(bool isFirst)
	{
		if(isFirst)
			ms_citCur = m_lstGraphicses.begin();
		else
			ms_citCur++;

		if(ms_citCur == m_lstGraphicses.end())
			return NULL;

		return (Pointer)*ms_citCur;
	}

	UINT Face::GetCount()
	{
		return m_lstGraphicses.size();
	}

	void Face::RefreshSize()
	{
		if(IsIgnoreSize())
			return;

		Rect _rArea1 = {0, 0, 0, 0};
		Rect _rArea2 = {0, 0, 0, 0};

		Graphics* _pG = NULL;
		for(GraphicsList::const_iterator _cit = m_lstGraphicses.begin();
			_cit != m_lstGraphicses.end(); _cit++)
		{
			_pG = *_cit;

			if(_pG->IsIgnoreSize() == false)
			{
				if(_pG->GetGType() == GRAPHICS_FACE)
					((Face*)_pG)->RefreshSize();

				_rArea2.Set(GAF_ARG_POINT(_pG->GetLocation_a()), GAF_ARG_SIZE(_pG->GetSize_a()));
				_rArea1 = _rArea1.Compose(_rArea2);
			}
		}

		SetSize(_rArea1.nWidth, _rArea1.nHeight);
	}
}
